package cn.gx.kevin.dao.impl;
import java.util.*;

import cn.gx.kevin.dao.IUserDao;
import cn.gx.kevin.dao.impl.BaseDao;
import cn.gx.kevin.domain.UserRole;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import cn.gx.kevin.domain.User;

/**
*Generate by Kevin's CodeBuilder
**/
@Repository(value="userDao")
public class UserDao extends BaseDao<User, String> implements IUserDao {

	private static final String  SQL_ID_LOAD_USERDATA = "loadUserData";
	
	@Override
	public void insertSuperAdmin(String name,String zhName,String pwd) throws Exception {
		User user = new User();
		user.setCreateTime(new Date());
		user.setCreateUserId("0");
		user.setId("0");
		user.setUserAccount(name);
		user.setUserName(zhName);
		user.setUserPwd(pwd);
		user.setUserStatus(1);
		user.setOrgId("0");
		this.insert(user);
	}

	@Override
	public User loadUserData(Map<String,Object> param) throws Exception {		
		return this.selectOne(SQL_ID_LOAD_USERDATA, param);
	}


	@Override
	public Map<String, List<UserRole>> selectUserRole(List<String> userIds) throws Exception {
		SqlSession session = this.getSqlSession();
		Map<String,Object> pars = new HashMap<>();
		pars.put("userIds",userIds);
		Map<String, List<UserRole>> userMap = new HashMap<>();
		session.select(namespace + ".selectUserRole", pars, new ResultHandler() {
			@Override
			public void handleResult(ResultContext resultContext) {
				Object obj =	resultContext.getResultObject();
				UserRole bean = (UserRole) obj;
				String id = bean.getUserId();
			    List<UserRole> roles =	userMap.get(id);
			    if(roles == null){
			    	roles = new ArrayList<>();
			    	userMap.put(id,roles);
				}
			    roles.add(bean);
			}
		});

		return userMap;
	}
}
